package com.syf.config.exception;

import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import com.syf.config.result.DefaultErrorResult;
import com.syf.config.result.RequestStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import java.util.List;

/**
 * @desc 全局异常处理基础类
 *
 * @author hqf
 */
@Slf4j
public abstract class BaseGlobalExceptionHandler {

	/**
	 * 违反约束异常
	 */
	protected DefaultErrorResult handleConstraintViolationException(ConstraintViolationException e, HttpServletRequest request) {
		log.error("handleConstraintViolationException start, uri:{}, caused by: ", request.getRequestURI(), e);
		List<ParameterInvalidItem> parameterInvalidItemList = ParameterInvalidItemHelper.convertCVSetToParameterInvalidItemList(e.getConstraintViolations());
		//返回全局异常对象
		return DefaultErrorResult.failure(RequestStatusEnum.PARAM_IS_INVALID, e, HttpStatus.BAD_REQUEST, parameterInvalidItemList);
	}

	/**
	 * 处理验证参数封装错误时异常
	 */
	protected DefaultErrorResult handleConstraintViolationException(HttpMessageNotReadableException e, HttpServletRequest request) {
		log.error("handleConstraintViolationException start, uri:{}, caused by: ", request.getRequestURI(), e);
		//返回全局异常对象
		return DefaultErrorResult.failure(RequestStatusEnum.PARAM_IS_INVALID, e, HttpStatus.BAD_REQUEST);
	}

	/**
	 * 处理参数绑定时异常
	 */
	protected DefaultErrorResult handleBindException(BindException e, HttpServletRequest request) {
		log.error("handleBindException start, uri:{}, caused by: ", request.getRequestURI(), e);
		List<ParameterInvalidItem> parameterInvalidItemList = ParameterInvalidItemHelper.convertBindingResultToMapParameterInvalidItemList(e.getBindingResult());
		//返回全局异常对象
		return DefaultErrorResult.failure(RequestStatusEnum.PARAM_IS_INVALID, e, HttpStatus.BAD_REQUEST, parameterInvalidItemList);
	}

	/**
	 * 处理使用@Validated注解时，参数验证错误异常
	 */
	protected DefaultErrorResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request) {
		log.error("handleMethodArgumentNotValidException start, uri:{}, caused by: ", request.getRequestURI(), e);
		List<ParameterInvalidItem> parameterInvalidItemList = ParameterInvalidItemHelper.convertBindingResultToMapParameterInvalidItemList(e.getBindingResult());
		//返回全局异常对象
		return DefaultErrorResult.failure(RequestStatusEnum.PARAM_IS_INVALID, e, HttpStatus.BAD_REQUEST, parameterInvalidItemList);
	}

	/**
	 * 处理通用自定义业务异常【自定义异常】
	 */
	protected ResponseEntity<DefaultErrorResult> handleBusinessException(BizException e, HttpServletRequest request) {
		log.error("异常全局处理：handleBizException start, uri:{}, exception:{}, caused by: {}", request.getRequestURI(), e.getClass(), e.getMessage(), e);
		DefaultErrorResult defaultErrorResult = DefaultErrorResult.failure(e);
		return ResponseEntity  //返回Json格式数据(可以自定义设置 数据主体、请求头、状态码)
				.status(HttpStatus.valueOf(defaultErrorResult.getStatus()))  //自定义异常码
				.body(defaultErrorResult);
	}

	/**
	 * 内部API调用的异常处理
	 */
	protected DefaultErrorResult handleApiException(ApiException e, HttpServletRequest request) {
		log.error("内部API调用的异常处理: handleApiException start, uri:{}, caused by: ", request.getRequestURI(), e);
		//返回全局异常对象
		return DefaultErrorResult.failure(RequestStatusEnum.PARAM_IS_INVALID, e, HttpStatus.BAD_REQUEST);
	}

	/**
	 * 处理未预测到的其他错误
	 */
	protected DefaultErrorResult handleThrowable(Throwable e, HttpServletRequest request) {
		log.error("handleThrowable start, uri:{}, caused by: ", request.getRequestURI(), e);
		return DefaultErrorResult.failure(RequestStatusEnum.REQUEEST_ERROR, e, HttpStatus.INTERNAL_SERVER_ERROR);
	}

}
